function price=Price_FCN(S0,K,B, r,coupon_r,mu, sigma, muJ, sigmaJ, lambda, T, Nstep, Ntraj,t)
%%%%%%%%%%%%%函数说明%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%运用蒙特卡罗方法计算FCN（固定票息票据）产品价格%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%输入参数：
%%%%%%%%%%S0：标的初始价格;K：产品执行价格;B：障碍价格;r：无风险利率;
%%%%%%%%%%coupon_r:连续复利计算的票息(合同约定利率);sigma：标的波动率;
%%%%%%%%%%muJ: 跳跃事件平均增长率;sigmaJ: 跳跃事件波动率;lambda: 跳跃事件强度;
%%%%%%%%%%Ntraj：模拟路径数；Nstep：每条路径一年的时间分割数
%%%%%%%%%%T：票据条款期限；t为敲出观察频率
%%%%%%%%%%特别说明:
%%%%%%%%%%当敲出频率为每天一次时，参数T=票据条款期限（天数）/365(闰年366)，
%%%%%%%%%%t=1/365;敲出频率为每个月时,参数T=票据条款期限（月数）/12,t=1/12.
%%%%%%%%%%输出参数：
%%%%%%%%%%Price:FCN价格;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    PricePath_M = merton_jump_diffusion(S0, mu, sigma, muJ, sigmaJ, lambda, ...
        T, Nstep, Ntraj);%一列为一条价格路径
    
    payoff=zeros(1,Ntraj);%为所有路径到期日回报预分配内存

    n = round((t/T).*Nstep).*(1:(T/t));%求出敲出观察日所对应的行
    
    %第一种情况(标的收盘价>=敲出价——视为触发敲出价格)
    out = PricePath_M(n,:)>=B; % 检验节点价格是否出现敲出
    outPath_1 = find(any(out, 1)); % 找到被敲出的路径列索引，1表示按照列方向查找
    
    outPath_2 = zeros(1, Ntraj); % 存储敲出路径对应的敲出时间索引
    %寻找第一次标的价格超过B时所对应时间
    for i = outPath_1
        idx = find(out(:, i), 1); % 找到第一个满足条件的行索引
        if ~isempty(idx)
            % 存储第一个满足条件的索引的最初索引，即其初始的行
            outPath_2(i) = round((t/T).*Nstep.*idx); 
        end
    end
    %第一种情况所对应的路径收益
    T1 = (outPath_2(outPath_1)/Nstep)*T; %敲出时该票息累计的时间T1
    payoff(outPath_1)=S0*(exp(coupon_r*T1)-1).*exp(-r*T1);%赚取费用的折现值

    %第二种情况(标的收盘价<敲出价&标的最终收盘价>K)
    %第三种情况(标的收盘价<敲出价&标的最终收盘价<K)
    remPath = setdiff(1:Ntraj,outPath_1);%找到未敲出路径索引
    cs = max(K-PricePath_M(end,remPath),0);
    payoff(remPath)=(S0.*(exp(T.*coupon_r)-1)-cs).*exp(-r*T);%赚取费用的折现值

    %求出FCN的价格
    price=mean(payoff);%计算所有路径收益均值
end